<?php
class EmailUtil{
     
	public static function sendEmail($subject,$to= null,$body = null,$attachment = null,$from = null){

        $body = trim($body);
        $footer= self::readFooterText();
        if($footer!==false){
            $body.='<br/><br/>'.$footer;
        }
		Yii::import('application.extensions.phpmailer.JPhpMailer');
        $mail   = new JPhpMailer;
        if($to == null){
            $mail->AddAddress('michael.bonza@qsibanking.com', "Michael Bonza");
        }   
		if(is_array($to)){
			foreach($to as $toAddress){
				$mail->AddAddress($toAddress);
			}
		}else{
			$mail->AddAddress($to);
		}
		
        
        $mail->SetFrom("ccserver@qsibanking.com", "QSI: Customer Connect");                        
        //$mail->AddReplyTo("ccserver@qsibanking.com", "QSI: Customer Connect");
        if(property_exists(Yii::app()->user, 'defaultEmail')){       
            $mail->AddReplyTo($user->defaultEmail);
            $mail->AddBCC($user->defaultEmail);           
            $mail->Sender = $user->defaultEmail;
        }else if(Yii::app()->user->isGuest == false){            
        	$user = Yii::app()->user->model;
        	$mail->AddReplyTo($user->email,$user->real_name);
            $mail->AddBCC($user->email,$user->real_name);
            //$mail->SetReturnPath("'$user->real_name' <{$user->email}>");
            $mail->Sender = $user->email;
        }
        
        $mail->Subject = $subject;
        $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
        $mail->MsgHTML($body);
        if($attachment !=null){
            if(is_array($attachment)){
                foreach($attachment as $a){
                    $mail->AddAttachment($a);
                }
            }else{
                $mail->AddAttachment($attachment);
            }
        }
        self::addUserAttachment($mail);
        $sentItem = new SentItems();
        $sentItem->from = 'ccserver@qsibanking.com';
        if(is_array($to)){
            $sentItem->to = implode(';',$to);
        }else{
            $sentItem->to = $to;
        }
        $sentItem->replyto = Yii::app()->user->model->email;
        $sentItem->message = $body;
        $sentItem->subject = $subject;
        $sentItem->create_date = date('Y-m-d H:i:s');
        $sentItem->rawmessage = $mail->CreateHeader();
        $sentItem->attachment='';
        if(is_array($attachment)){
            $sentItem->attachment = implode(';',$attachment);    
        }else{
            $sentItem->attachment = $attachment;
        }
        if(count(self::$uploadedFiles>0)){
            foreach(self::$uploadedFiles as $file){
                $sentItem->attachment.=$file.';';    
            }
            
        }
        
        $sentItem->url = Yii::app()->request->hostInfo . Yii::app()->request->url;
        $sentItem->save(); 
        $mail->AddCustomHeader('CC-Id:'.$sentItem->id);

        
        $rs = $mail->Send();
        self::deleteUploadedFile();
        if($rs){
        	               
        	return true;
        }else{
            $sentItem->delete();
        	return false;
        }
    }

    protected static function readFooterText(){
        $filename ='email_footer.txt';
        $f = fopen($filename,'r');
        if($f!==false){
            $content = fread($f,filesize($filename));
            fclose($f);
            return $content;
        }
        return false;
    }

    static $uploadedFiles = array();
    protected static function addUserAttachment($mail){
        $uploaded = CUploadedFile::getInstanceByName('attachment');
        if($uploaded!=null && !in_array($uploaded->extensionName,array('exe','rar','zip'),true)){
            $name = 'documents/'.time().'.'.$uploaded->extensionName;
            $uploaded->saveAs($name,true);
            $mail->AddAttachment($name);
            self::$uploadedFiles[] = $name;
            //@unlink($name);
        }
    }

    protected static function deleteUploadedFile(){
        foreach(self::$uploadedFiles as $file){
            @unlink($file);
        }
    }
}